//
//  JCTabBarController.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/9.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

private var TABBAR_INDEX_KEY = UnsafePointer<Void>()

class JCTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //赋值给系统的tabbar
        let tab = JCTabBar()
        tab.plusButtonBlock = {
            print("plusButton按钮点击")
            
            
            //初始化composeView
            let compseView = JCComposeView()
            //显示
            compseView.show(target: self)
                   }
        setValue(tab, forKey: "tabBar")
     
        
        addChildViewController(JCHomeTableViewController(), title: "首页", imageName: "tabbar_home", index: 0)
        addChildViewController(JCMessageTableViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(JCDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover", index: 2)
        addChildViewController(JCProfileTableViewController(), title: "我", imageName: "tabbar_profile", index: 3)
        
    }

   //添加子控制器
    private func addChildViewController(childController: UIViewController,title: String, imageName: String, index: Int) {
        let item = JCTabBarItem()
        //给tabbarItem设置index ,代表当前第几个
//        item.index = index
        
        //设置 index 对象关联
        objc_setAssociatedObject(item, &TABBAR_INDEX_KEY, NSNumber(integer: index), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        
        //设置成自定义的tabbarItem
        childController.tabBarItem = item
        
        //设置标题
        childController.title = title
        //设置图片,被选择
            //设置渲染模式
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置偏移量 改变tabbar图片位置
        childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        
        //设置文字的属性
            //设置选中文字颜色
        let textColorAttr = [NSForegroundColorAttributeName:UIColor.orangeColor()]
        childController.tabBarItem.setTitleTextAttributes(textColorAttr, forState: UIControlState.Selected)
        
        //设置导航控制器
        let nav = JCNavigationController(rootViewController: childController)
        addChildViewController(nav)
        
    }
    
    // MARK: - 设置点击tabbaritem的动画
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        printLog(item.title!)
        //设置自定义item
//        let jcItem = item as! JCTabBarItem
        
        
        //遍历得到被点击的item执行动画
        var index = 0
        //得到tabbar子类  tabbarBtn
        for tabBarChild in tabBar.subviews{
            if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                for tabBarBtnChild in tabBarChild.subviews{
                    //根据对象关联 得到下标值
                    let resultIndex = objc_getAssociatedObject(item, &TABBAR_INDEX_KEY)
                    //执行动画
//                    if index == jcItem.index{
                    if index == (resultIndex as! Int){
                        if tabBarBtnChild.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                        //得到设置动画的控件
                            tabBarBtnChild.transform = CGAffineTransformMakeScale(0.8, 0.8)
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                                tabBarBtnChild.transform = CGAffineTransformIdentity
                                }, completion: { (finish) -> Void in
                                    
                            })
                        }
                    }
                }
                //
                index++
            }
        }
    }
}
